package com.shanghaionstar.share;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.onstar.cn.common.util.PayMeConstants;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.share.ShareFootPrintDialog;
import com.shanghaionstar.utils.BusinessUtil;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * Created by 6335yz on 2016/7/28.
 */
public abstract  class AbstractShareDialog extends Dialog {

    protected Context context;

    protected Button btn_share_cancel;
    protected CustomImageView btn_share_wechat_friend;
    protected CustomImageView btn_share_wx_moments;

    protected IWXAPI weiXinApi;// 是第三方app和微ssss信通信的接口

    public AbstractShareDialog(Context context) {
        super(context);
        init(context);
    }

    public AbstractShareDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public AbstractShareDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        registerWXAPI();
        setContentView(context);
        setUpWindow();
        setCanceledOnTouchOutside(true);
    }

    private void setUpWindow(){
        Window window = getWindow();
        window.setWindowAnimations(R.style.share_layout_animation);
//        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams attrs = new WindowManager.LayoutParams();
        attrs.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attrs.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        attrs.gravity = Gravity.BOTTOM;
        window.setAttributes(attrs);
    }

    private void registerWXAPI() {
        weiXinApi = WXAPIFactory.createWXAPI(context, PayMeConstants.APP_ID, true);
        // 将应用的appid注册到微信
        weiXinApi.registerApp(PayMeConstants.APP_ID);
    }

    private void setContentView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_share_layout, null);
        bindViews(view);
        setListeners();
        setContentView(view);
    }

    private void bindViews(View parent) {
        btn_share_wechat_friend = (CustomImageView) parent.findViewById(R.id.btn_share_wechat_friend);
        btn_share_wx_moments = (CustomImageView) parent.findViewById(R.id.btn_share_wx_moments);
        btn_share_cancel = (Button) parent.findViewById(R.id.btn_share_cancel);
    }

    private String reportCancel = "",wechat = "",moments = "";//报告：取消，微信，朋友圈

    public void setReportCancel(String reportCancel) {
        this.reportCancel = reportCancel;
    }

    public void setWechat(String wechat) {
        this.wechat = wechat;
    }

    public void setMoments(String moments) {
        this.moments = moments;
    }

    private void setListeners() {
        btn_share_wechat_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(context, wechat);
                shareToWX();
                cancel();
            }
        });
        btn_share_wx_moments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(context, moments);
                shareToWXF();
                cancel();
            }
        });
        btn_share_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(context, reportCancel);
                if(AbstractShareDialog.this instanceof ShareFootPrintDialog){
                    BusinessUtil.sendReportByClickType(context, FunctionNameFindId.Footprint_Cancel.getStringValue());
                }
                cancel();
            }
        });
    }

    //分享到微信好友
    abstract public void shareToWX() ;

    //微信朋友圈
    abstract public void shareToWXF();
}
